package com.sinaapp.gavinzhang.GSpider.config;

import com.sinaapp.gavinzhang.GSpider.DefaultPageProcessor;
import com.sinaapp.gavinzhang.GSpider.core.PageProcessor;
import com.sinaapp.gavinzhang.GSpider.process.DefaultHttpProcessorInterceptor;
import com.sinaapp.gavinzhang.GSpider.process.HttpProcessorInterceptor;
import com.sinaapp.gavinzhang.GSpider.webaddress.URLDispatcher;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author gavin
 * @version 1.0
 * @date 2015/9/2
 * @description 配置对象bean
 */
public class PageProcessorConfig implements Serializable {
    private static final long serialVersionUID = 4077329331699980334L;

    /**
     * 网址获取服务
     */
    private URLDispatcher urlDispatcher;

    /**
     * 符合要求的网址正则表达
     */
    private List<Pattern> patterns;
    /**
     * 设置处理目标函数
     */
    private PageProcessor pageProcessor;

    /**
     * 网页获取拦截器
     * 以后要改用list
     */
    private HttpProcessorInterceptor httpProcessorInterceptor;

    private int timeout = Constants.TIMEOUT;//最大重复次数
    private int sleepTime = Constants.SLEEPTIME;//如果太快初始睡眠时间
    private int waitTime = Constants.WAITTIME;//默认等待时间，现在还未启用


    @Deprecated
    public static PageProcessorConfig getDefaultPageProcessorConfig() {
        PageProcessorConfig pageProcessorConfig = new PageProcessorConfig();
        pageProcessorConfig.urlDispatcher = null;
        pageProcessorConfig.patterns = new ArrayList<>();
        pageProcessorConfig.pageProcessor = new DefaultPageProcessor();
        pageProcessorConfig.httpProcessorInterceptor = new DefaultHttpProcessorInterceptor();
        pageProcessorConfig.timeout = Constants.TIMEOUT;
        pageProcessorConfig.sleepTime = Constants.SLEEPTIME;
        pageProcessorConfig.waitTime = Constants.WAITTIME;
        return pageProcessorConfig;
    }

    public static PageProcessorConfig newPageProcessorConfig(){
        PageProcessorConfig pageProcessorConfig = new PageProcessorConfig();
        pageProcessorConfig.urlDispatcher = null;
        pageProcessorConfig.patterns = new ArrayList<>();
        pageProcessorConfig.pageProcessor = new DefaultPageProcessor();
        pageProcessorConfig.httpProcessorInterceptor = new DefaultHttpProcessorInterceptor();
        pageProcessorConfig.timeout = Constants.TIMEOUT;
        pageProcessorConfig.sleepTime = Constants.SLEEPTIME;
        pageProcessorConfig.waitTime = Constants.WAITTIME;
        return pageProcessorConfig;
    }

    public HttpProcessorInterceptor getHttpProcessorInterceptor() {
        return httpProcessorInterceptor;
    }

    public void setHttpProcessorInterceptor(HttpProcessorInterceptor httpProcessorInterceptor) {
        this.httpProcessorInterceptor = httpProcessorInterceptor;
    }


    public List<Pattern> getPatterns() {
        return patterns;
    }

    public void setPatterns(List<Pattern> patterns) {
        this.patterns = patterns;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getSleepTime() {
        return sleepTime;
    }

    public void setSleepTime(int sleepTime) {
        this.sleepTime = sleepTime;
    }

    public int getWaitTime() {
        return waitTime;
    }

    public void setWaitTime(int waitTime) {
        this.waitTime = waitTime;
    }
    /**
     * 改为pattens
     */
    @Deprecated
    public List<Pattern> getListP() {
        return patterns;
    }

    /**
     * 改为pattens
     */
    @Deprecated
    public void setListP(List<Pattern> listP) {
        this.patterns = listP;
    }

    public PageProcessor getPageProcessor() {
        return pageProcessor;
    }

    public void setPageProcessor(PageProcessor pageProcessor) {
        this.pageProcessor = pageProcessor;
    }

    @Deprecated
    public List<Pattern> getPatternUrls() {
        return patterns;
    }

    @Deprecated
    public void setPatternUrls(List<Pattern> patternUrls) {
        this.patterns = patternUrls;
    }

    public URLDispatcher getUrlDispatcher() {
        return urlDispatcher;
    }

    public void setUrlDispatcher(URLDispatcher urlDispatcher) {
        this.urlDispatcher = urlDispatcher;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PageProcessorConfig that = (PageProcessorConfig) o;

        if (timeout != that.timeout) return false;
        if (sleepTime != that.sleepTime) return false;
        if (waitTime != that.waitTime) return false;
        if (patterns != null ? !patterns.equals(that.patterns) : that.patterns != null) return false;
        if (pageProcessor != null ? !pageProcessor.equals(that.pageProcessor) : that.pageProcessor != null)
            return false;
        return !(httpProcessorInterceptor != null ? !httpProcessorInterceptor.equals(that.httpProcessorInterceptor) : that.httpProcessorInterceptor != null);

    }

    @Override
    public int hashCode() {
        int result = patterns != null ? patterns.hashCode() : 0;
        result = 31 * result + (pageProcessor != null ? pageProcessor.hashCode() : 0);
        result = 31 * result + (httpProcessorInterceptor != null ? httpProcessorInterceptor.hashCode() : 0);
        result = 31 * result + timeout;
        result = 31 * result + sleepTime;
        result = 31 * result + waitTime;
        return result;
    }
}
